namespace TSL.Utility.Json
{
    using System;
    using System.Collections.Specialized;
    using System.Configuration;
    using System.Xml;

    internal class AjaxSettingsSectionHandler : IConfigurationSectionHandler
    {
        public object Create(object parent, object configContext, XmlNode section)
        {
            AjaxSettings settings = new AjaxSettings();
            Utility.AddDefaultConverter(settings);
            foreach (XmlNode node in section.ChildNodes)
            {
                if (node.Name == "jsonConverters")
                {
                    XmlNode node3;
                    Type type;
                    if ((node.SelectSingleNode("@includeTypeProperty") != null) && (node.SelectSingleNode("@includeTypeProperty").InnerText == "true"))
                    {
                        settings.IncludeTypeProperty = true;
                    }
                    XmlNodeList list = node.SelectNodes("add");
                    foreach (XmlNode node2 in list)
                    {
                        node3 = node2.SelectSingleNode("@type");
                        if (node3 != null)
                        {
                            type = Type.GetType(node3.InnerText);
                            if ((type != null) && typeof(IJavaScriptConverter).IsAssignableFrom(type))
                            {
                                StringDictionary d = new StringDictionary();
                                foreach (XmlAttribute attribute in node2.Attributes)
                                {
                                    if (!d.ContainsKey(attribute.Name))
                                    {
                                        d.Add(attribute.Name, attribute.Value);
                                    }
                                }
                                IJavaScriptConverter converter = (IJavaScriptConverter) Activator.CreateInstance(type);
                                converter.Initialize(d);
                                Utility.AddConverter(settings, converter, true);
                            }
                        }
                    }
                    list = node.SelectNodes("remove");
                    foreach (XmlNode node2 in list)
                    {
                        node3 = node2.SelectSingleNode("@type");
                        if (node3 != null)
                        {
                            type = Type.GetType(node3.InnerText);
                            if (type != null)
                            {
                                Utility.RemoveConverter(settings, type);
                            }
                        }
                    }
                }
            }
            return settings;
        }
    }
}

